﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for CommonHelper
/// </summary>
public class CommonHelper
{
    public CommonHelper()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static void SetCookie(string cookieName, string cookieValue, TimeSpan ts)
    {
        try
        {
            HttpCookie cookie = new HttpCookie(cookieName)
            {
                Value = HttpContext.Current.Server.UrlEncode(cookieValue)
            };
            cookie.Expires = DateTime.Now.Add(ts);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        catch (Exception exception)
        {
            System.Diagnostics.Debug.WriteLine(exception.Message);
        }
    }

    public static bool GetCookieBool(string cookieName)
    {
        string str = GetCookieString(cookieName, true).ToUpperInvariant();
        return (((str == "TRUE") || (str == "YES")) || (str == "1"));
    }

    public static int GetCookieInt(string cookieName)
    {
        string cookieString = GetCookieString(cookieName, true);
        if (!string.IsNullOrEmpty(cookieString))
        {
            return Convert.ToInt32(cookieString);
        }
        return 0;
    }

    public static string GetCookieString(string cookieName, bool decode)
    {
        if (HttpContext.Current.Request.Cookies[cookieName] == null)
        {
            return string.Empty;
        }
        try
        {
            string s = HttpContext.Current.Request.Cookies[cookieName].Value.ToString();
            if (decode)
            {
                s = HttpContext.Current.Server.UrlDecode(s);
            }
            return s;
        }
        catch
        {
            return string.Empty;
        }
    }

    public static string QueryString(string name)
    {
        string str = string.Empty;
        if ((HttpContext.Current != null) && (HttpContext.Current.Request.QueryString[name] != null))
        {
            str = HttpContext.Current.Request.QueryString[name].ToString();
        }
        return str;
    }
    public static string QueryString(string name, string defaulvalue)
    {
        string str = defaulvalue;
        if ((HttpContext.Current != null) && (HttpContext.Current.Request.QueryString[name] != null))
        {
            str = HttpContext.Current.Request.QueryString[name].ToString();
        }
        return str;
    }

    public static bool QueryStringBool(string name)
    {
        string str = QueryString(name).ToUpperInvariant();
        return (((str == "YES") || (str == "TRUE")) || (str == "1"));
    }
    public static bool? QueryStringBoolNullable(string name)
    {
        string str = QueryString(name).ToUpperInvariant();
        if (str.Equals("") || str.Equals("-1"))
            return null;
        return (((str == "YES") || (str == "TRUE")) || (str == "1"));
    }
    public static bool? StringToBoolNullable(string name)
    {
        string str =name;
        if (str.Equals("") || str.Equals("-1"))
            return null;
        return (((str == "YES") || (str == "TRUE")) || (str == "1"));
    }
    /// <summary>
    /// true = 1, false = 0, null = -1
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string BoolToIntString(bool? obj)
    {
        return (obj.HasValue ? (obj.Value == true ? 1 : 0) : -1).ToString();
    }
    public static Guid? QueryStringGuid(string name)
    {
        string g = QueryString(name).ToUpperInvariant();
        Guid? nullable = null;
        try
        {
            nullable = new Guid(g);
        }
        catch
        {
        }
        return nullable;
    }

    public static int QueryStringInt(string name)
    {
        int num;
        int.TryParse(QueryString(name).ToUpperInvariant(), out num);
        return num;
    }

    public static int QueryStringInt(string name, int defaultValue)
    {
        string s = QueryString(name).ToUpperInvariant();
        if (s.Length > 0)
        {
            return int.Parse(s);
        }
        return defaultValue;
    }
    /// <summary>
    /// -1 => defaulvalue,"" => defaultValue
    /// </summary>
    /// <param name="name"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static int? QueryStringInt(string name, int? defaultValue)
    {
        string s = QueryString(name).ToUpperInvariant();
        if (s.Equals("") || s.Equals("-1"))
            return defaultValue;
        if (s.Length > 0)
        {
            return int.Parse(s);
        }
        return defaultValue;
    }
    public static bool IsValidEmail(string email)
    {
        bool flag = false;
        if (string.IsNullOrEmpty(email))
        {
            return flag;
        }
        email = email.Trim();
        return Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
    }
    public static string GenerateRandomDigitCode(int length)
    {
        Random random = new Random();
        string str = string.Empty;
        for (int i = 0; i < length; i++)
        {
            str = str + random.Next(10).ToString();
        }
        return str;
    }

    public static string GetFormString(string name)
    {
        string str = string.Empty;
        if ((HttpContext.Current != null) && (HttpContext.Current.Request[name] != null))
        {
            str = HttpContext.Current.Request[name].ToString();
        }
        return str;
    }

}
